"""
商品和评价相关模型
"""
from sqlalchemy import Column, BigInteger, String, Text, Integer, DateTime, ForeignKey, DECIMAL
from sqlalchemy.orm import relationship
from datetime import datetime
from database.base import Base


class Goods(Base):
    """商品表"""
    __tablename__ = "goods"
    
    id = Column(BigInteger, primary_key=True, index=True, comment="商品ID")
    name = Column(String(200), nullable=False, comment="商品名称")
    description = Column(Text, comment="商品描述")
    price = Column(DECIMAL(10, 2), comment="商品价格")
    image_url = Column(String(500), comment="商品图片URL")
    category = Column(String(100), comment="商品分类")
    stock = Column(Integer, default=0, comment="库存数量")
    status = Column(Integer, default=1, comment="状态：0-下架，1-上架")
    created_at = Column(DateTime, default=datetime.now, comment="创建时间")
    updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment="更新时间")
    
    # 关联关系
    reviews = relationship("GoodsReview", back_populates="goods", cascade="all, delete-orphan")


class GoodsReview(Base):
    """商品评价表"""
    __tablename__ = "goods_reviews"
    
    id = Column(BigInteger, primary_key=True, index=True, comment="评价ID")
    user_id = Column(BigInteger, ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True, comment="用户ID")
    goods_id = Column(BigInteger, ForeignKey("goods.id", ondelete="CASCADE"), nullable=False, index=True, comment="商品ID")
    content = Column(Text, nullable=False, comment="评价内容")
    score = Column(DECIMAL(3, 1), nullable=False, comment="评分（0-10）")
    created_at = Column(DateTime, default=datetime.now, index=True, comment="创建时间")
    updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment="更新时间")
    
    # 关联关系
    user = relationship("User", foreign_keys=[user_id])
    goods = relationship("Goods", back_populates="reviews")

